<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Person;

class UserController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
*/
    public function __construct(Request $request)
    {
        $this->middleware('auth');

    }

    public function index(Request $request)
    {
        $where = array(1,2,3);
        $keyword = $request->input('keyword');
        $user = User::orderBy("id", "asc");
        if (! empty($keyword)) {
            $user = $user->where('name', 'like', "%$keyword%")->orwhere('email', 'like', "%$keyword%");

        }

        return view('user/index')->withUsers($user->paginate(15));
    }

    public function create()
    {

        return view('user/create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required|string|between:6,12',
            'email' => 'required|email|string|between:10,20',
            'password' => 'required|string|between:6,20|confirmed',
        ]);

        $user = new User();

        $user->name = $request->input('name');
        $user->email = $request->input('email');
        $user->password = bcrypt($request->input('password'));

        if ($user->save()) {
//            return view('jump')->with([
//                'message'=>'添加管理员成功',
//                'url' =>url('user'),
//                'jumpTime'=>2,
//                'pageName'=> "管理员列表"
//            ]);
            //return redirect('user');
            return redirect('user/')->with('success', '添加管理员成功');
        } else {
            return redirect()->back()->withInput()->withErrors( );
        }
    }

    public function messages()
    {
        return [
            'title.required' => 'A title is required',
            'body.required'  => 'A message is required',
        ];
    }

    public function edit($id, Request $request)
    {
        $user =  Auth::user($id);
        if (! $user) {
//            return view('jump')->with([
//                'message'=>'用户不存在',
//                'url' =>url('user'),
//                'jumpTime'=>2,
//                'pageName'=> "管理员列表"
//            ]);
                return response(view('errors.404'), 404);

        }
        if($id == 0) {
            $id = $request->user()->id;
        }


        return view('user/edit')->withUser($user);
    }

    public function update(Request $request, $id)
    {
        $id = $request->input('id');
        $user = User::find($id);

        if (! $user) {
            return response(view('errors.404'), 404);
        }

        if ($user->id == 1 && $id != 1) {
            return response(view('errors.403'), 403);
        }


        $this->validate($request, [
           // 'password' => 'required|confirmed|string|between:6,12',
            //'password_confirmation' => 'required',
            'password' => 'required|confirmed|string|between:6,20',
        ]);


        $user->password = bcrypt($request->input('password'));

        if ($user->save()) {

            return redirect('user/' . $id . '/edit')->with('success', '修改密码成功');
        } else {
            return redirect()->back()->withInput()->withErrors('保存失败！');
        }

    }

    public function destroy($id)
    {
        if ($id)
            $id = explode(',', $id);
        else
            $id = '0';
        //var_dump($id);
        $data = Person::whereIn('id', $id)->delete();
        //var_dump($data);
        return redirect()->back();
    }



    // 禁用解锁管理员
    public function forbid(Request $request)
    {
        //return false;
        $id = $request->input('id');
        $type = $request->input('type');
        if($id == 1) {

            return redirect()->back()->withInput()->withErrors('初始管理员不能禁用！');
        }

        if ($id)
            $id = explode(',', $id);
        else
            $id = '0';

        //var_dump($id);
        if ($type == 1) {
            $data = User::whereIn('id', $id)->update(["is_forbided" => 1]);
        } else {
            $data = User::whereIn('id', $id)->update(["is_forbided" => 0]);
        }
        //var_dump($data);
        return redirect()->back();
    }
}
